package cn.wk.pet.controller;

import cn.wk.basic.utils.AjaxResult;
import cn.wk.basic.utils.PageBean;
import cn.wk.basic.utils.SystemContext;
import cn.wk.org.domain.Shop;
import cn.wk.org.mapper.ShopMapper;
import cn.wk.org.service.IShopService;
import cn.wk.pet.domain.Pet;
import cn.wk.pet.query.PetQuery;
import cn.wk.pet.service.IPetService;
import cn.wk.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private IPetService petService;

    /**
     * 待处理寻主信息点击处理按键
     * 保存数据到pet表中去
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/pendingMessage")
    public AjaxResult pending(@RequestBody Pet pet, HttpServletRequest request){
        try{
            //通过token获取shop
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            Shop shop = shopService.findByLoginInfoId(loginInfo.getId());
            //把shop放到pet中去
            pet.setShop(shop);
            petService.handleSave(pet,loginInfo);
            return new AjaxResult();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 查找宠物维护信息
     * @param petQuery
     * @param request
     * @return
     */
    @PostMapping
    public PageBean<Pet> findPet(@RequestBody PetQuery petQuery,HttpServletRequest request){
        //获取当前登录对象logininfo
        LoginInfo loginInfo = SystemContext.getLoginInfo(request);
        //获取shop
        Shop shop = shopService.findByLoginInfoId(loginInfo.getId());
        petQuery.setShopId(shop.getId());
        return petService.queryAll(petQuery);
    }

    /**
     * 添加/修改数据
     * @param pet
     * @return
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Pet pet,HttpServletRequest request){
        try{
            //获取shop
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            Shop shop = shopService.findByLoginInfoId(loginInfo.getId());
            pet.setShop(shop);
            if(pet.getId() == null){
                petService.savePet(pet);
            }else {
                petService.update(pet);
            }
            return new AjaxResult();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }




    //----------------------------以下是商城页面--------------------------------------------
    @PostMapping("/findPets")
    public PageBean<Pet> goPet(@RequestBody PetQuery petQuery){
        return petService.queryAll(petQuery);
    }

    @GetMapping("/{id}")
    public Pet findById(@PathVariable("id") Long id){
        return petService.findById(id);
    }
}
